/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package scenic.toolkit.db;

public class CharArray extends Array {
	private int[] data;

	public CharArray() {
		super(new ArrayType(AtomicType.charType, 0));
	}
	
	public CharArray(String string) {
		super(new ArrayType(AtomicType.charType, 0));
		int s = string.length();
		
		setSize(s);
		for(int i = 0; i < s; i++)
			data[i] = string.charAt(i);
		
	}
	
	protected Object getData() {
		return data;
	}
	
	protected void setData(Object data) {
		this.data = (int[])data;
	}
	
	protected Object allocate(int size) {
		return new int[size];
	}
	
	public void write(int position, int[] insertData) {
		super.write(position, insertData, insertData.length);
	}
	
	public void insert(int position, int[] insertData) {
		super.insert(position, insertData, insertData.length);
	}
	
	public void set(int index, int value) {
		if(index >= getLength())
			throw new IndexOutOfBoundsException();
		data[index] = value;
		super.notifyWrite(index, 1);
	}
	
	public int get(int index) {
		if(index >= getLength())
			throw new IndexOutOfBoundsException();
		return data[index];
	}
	
	public String toString() {
		return new String(data, 0, getLength());
	}
	
	public void storeTo(StorageOutputStream os) {
		os.write(this);
	}
}
